package com.geek.gkfgw.service.token.bean;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

/**
 * <h1>令牌信息对象。</h1><br/>
 *
 * <h1><br/><a href="https://u.geekbang.org/subject/arch3/100614201">Java 业务架构实战营：https://u.geekbang.org/subject/arch3/100614201</a></h1><br/><h1><a href="https://gitee.com/ylimhhmily/BizArchPracticeCamp">Java 业务架构实战营代码地址：https://gitee.com/ylimhhmily/BizArchPracticeCamp</a></h1><br/><h1><a href="https://u.geekbang.org/subject/java4th/1001148?source=app_share">高级 Java 工程体系课 2023 版：https://u.geekbang.org/subject/java4th/1001148?source=app_share</a></h1><br/><h1><a href="https://time.geekbang.org/column/intro/100312101?tab=catalog">Dubbo 源码剖析与实战：https://time.geekbang.org/column/intro/100312101?tab=catalog</a></h1>
 *
 * @author hmilyylimh
 * ^_^
 * @version 0.0.1
 * ^_^
 * @date 2023/10/15 16:04
 */
@Setter
@Getter
@ToString(callSuper = true)
public class TokenInfo implements Serializable {

    private static final long serialVersionUID = 6583770533675556609L;

    /** <h2>过期时间，例：1693733072746</h2> **/
    private Long expireTimeMillis;

    /** <h2>用户ID，例：100000001</h2> **/
    private String userId;
    /** <h2>用户姓名，例：张三</h2> **/
    private String userName;

    /**
     * <h2>创建一个失效的空对象。</h2>
     *
     * @param:
     * @return: com.geek.gkfgw.service.token.bean.TokenInfo
     */
    public static TokenInfo generateInvalidTokenInfo() {
        return new TokenInfo();
    }

    /**
     * <h2>判断当前对象是否过期。</h2>
     *
     * @param:
     * @return: boolean：<h2>{@code true}：已过期；{@code false}：未过期。</h2>
     */
    public boolean isExpired() {
        if (expireTimeMillis == null) {
            return true;
        }

        return System.currentTimeMillis() > expireTimeMillis;
    }

    /**
     * <h2>判断当前对象是否生效，即是否还在有效期内。</h2>
     *
     * @param:
     * @return: boolean：<h2>{@code true}：生效数据，即未过期；{@code false}：失效数据，即已过期。</h2>
     */
    public boolean isAlived() {
        return (expireTimeMillis != null && expireTimeMillis > System.currentTimeMillis());
    }
}